package com.lanou.wuyou.gift;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioGroup;

import com.lanou.wuyou.gift.category.CategoryFragment;
import com.lanou.wuyou.gift.gift.GiftFragment;
import com.lanou.wuyou.gift.home.HomeFragment;
import com.lanou.wuyou.gift.list.ListFragment;
import com.lanou.wuyou.gift.my.MyFragment;
import com.umeng.socialize.UMShareAPI;

public class MainActivity extends AppCompatActivity {

    private HomeFragment homeFragment;
    private CategoryFragment categoryFragment;
    private FragmentManager manager;
    private FragmentTransaction transaction;
    private ListFragment listFragment;
    private GiftFragment giftFragment;
    private MyFragment myFragment;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFragment();
        selectFragment();

    }

    //初始化Fragment
    private void initFragment() {
        homeFragment = HomeFragment.newInstance();
        categoryFragment = CategoryFragment.newInstance();
        listFragment = ListFragment.newInstance();
        giftFragment = GiftFragment.newInstance();
        myFragment = MyFragment.newInstance();
        //不需要重复获取
        manager = getSupportFragmentManager();
    }

    //注册监听,切换fragment
    private void selectFragment() {
        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //需要每次重新获取一次(manager获取一次即可)
                transaction = manager.beginTransaction();

                switch (checkedId) {
                    case R.id.home_rb:
                        transaction.replace(R.id.replace_view,homeFragment);
                        break;
                    case R.id.classify_rb:
                        transaction.replace(R.id.replace_view,categoryFragment);
                        break;
                    case R.id.gift_rb:
//                        transaction.replace(R.id.replace_view,giftFragment);
                        break;
                    case R.id.list_rb:
                        transaction.replace(R.id.replace_view,listFragment);
                        break;
                    case R.id.my_rb:
                        transaction.replace(R.id.replace_view,myFragment);
                        break;
                }
                transaction.commit();

            }
        });
        //设置默认选中的btn
        radioGroup.check(R.id.home_rb);
    }

}
